package com.itheima.protocol.serializable;

import java.io.*;


/**
 * 序列化不保存静态变量
 * 被transient关键字修饰的属性，不参与序列化
 */
public class SerializableDemo {

    public static void main(String[] args) {

        //序列化
        serializePerson();
        Person.height = 16;

        //反序列化
        Person person = DeserializePerson();
        System.out.println(person);
    }

    private static void serializePerson(){

        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(new File("person")));

            Person person = new Person();
            person.setAge(18);
            person.setName("wql");
            outputStream.writeObject(person);

            System.out.println("序列化完成");

            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static Person DeserializePerson() {

        try {
            ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("person")));

            Person person = (Person)inputStream.readObject();
            System.out.println("反序列化成功");
            System.out.println(person.toString());

            return person;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
